<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="书名" prop="bookTitle">
        <el-input v-model="queryParams.bookTitle" placeholder="请输入书名" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="委托人" prop="userName">
        <el-input v-model="queryParams.userName" placeholder="请输入委托人" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="开始时间" prop="deliveryPeriodStart">
        <el-date-picker clearable v-model="queryParams.deliveryPeriodStart" type="date" value-format="yyyy-MM-dd"
          placeholder="请选择开始时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="截止时间" prop="deliveryPeriodEnd">
        <el-date-picker clearable v-model="queryParams.deliveryPeriodEnd" type="date" value-format="yyyy-MM-dd"
          placeholder="请选择截止时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="目的地" prop="deliveryLocation">
        <el-input v-model="queryParams.deliveryLocation" placeholder="请输入目的地" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="接单人" prop="acceptorName">
        <el-input v-model="queryParams.acceptorName" placeholder="请输入接单人" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择状态" clearable>
          <el-option v-for="dict in dict.type.book_commission" :key="dict.value" :label="dict.label"
            :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="评分" prop="rating">
        <el-input v-model="queryParams.rating" placeholder="请输入评分" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <!-- 学校选择列表 -->
      <el-form-item label="学校" prop="institutionId">
        <el-select v-model="queryParams.institutionId" filterable placeholder="请选择学校" style="width: 100%">
          <el-option v-for="item in institutionList" :key="item.institutionId" :label="item.institutionName"
            :value="item.institutionId" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['book:commission:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['book:commission:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['book:commission:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['book:commission:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="commissionList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column :show-overflow-tooltip="true" label="委托ID" align="center" prop="commissionId" />
      <el-table-column :show-overflow-tooltip="true" label="书名" align="center" prop="bookTitle" />
      <el-table-column :show-overflow-tooltip="true" label="索书号" align="center" prop="callNumber" />
      <el-table-column :show-overflow-tooltip="true" label="条码号" align="center" prop="barcode" />
      <el-table-column :show-overflow-tooltip="true" label="出版社" align="center" prop="publisherInfo" />
      <el-table-column :show-overflow-tooltip="true" label="ISBN" align="center" prop="isbn" />
      <el-table-column :show-overflow-tooltip="true" label="封面" align="center" prop="bookCoverUrl" />
      <el-table-column :show-overflow-tooltip="true" label="书籍位置" align="center" prop="locationInfo" />
      <el-table-column :show-overflow-tooltip="true" label="委托人ID" align="center" prop="userId" />
      <el-table-column :show-overflow-tooltip="true" label="委托人" align="center" prop="userName" />
      <el-table-column :show-overflow-tooltip="true" label="委托电话" align="center" prop="userPhone" />
      <el-table-column label="开始时间" align="center" prop="deliveryPeriodStart" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.deliveryPeriodStart, '{y}-{m}-{d} {h}:{i}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="截止时间" align="center" prop="deliveryPeriodEnd" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.deliveryPeriodEnd, '{y}-{m}-{d} {h}:{i}') }}</span>
        </template>
      </el-table-column>
      <el-table-column :show-overflow-tooltip="true" label="目的地" align="center" prop="deliveryLocation" />
      <el-table-column :show-overflow-tooltip="true" label="备注" align="center" prop="notes" />
      <el-table-column :show-overflow-tooltip="true" label="接单人ID" align="center" prop="acceptorId" />
      <el-table-column :show-overflow-tooltip="true" label="接单学号" align="center" prop="acceptorStudentId" />
      <el-table-column :show-overflow-tooltip="true" label="接单人" align="center" prop="acceptorName" />
      <el-table-column :show-overflow-tooltip="true" label="接单电话" align="center" prop="acceptorPhone" />
      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.book_commission" :value="scope.row.status" />
        </template>
      </el-table-column>
      <el-table-column label="评分" align="center" prop="rating" />
      <el-table-column label="评价" align="center" prop="review" />
      <el-table-column label="创建时间" align="center" prop="createdAt" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createdAt, '{y}-{m}-{d} {h}:{i}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="更新时间" align="center" prop="updatedAt" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.updatedAt, '{y}-{m}-{d} {h}:{i}') }}</span>
        </template>
      </el-table-column>

      <el-table-column label="学校" align="center" prop="institutionName" /> -->

      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['book:commission:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['book:commission:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改图书委托对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="书名" prop="bookTitle">
          <el-input v-model="form.bookTitle" placeholder="请输入书名" />
        </el-form-item>
        <el-form-item label="索书号" prop="callNumber">
          <el-input v-model="form.callNumber" placeholder="请输入索书号" />
        </el-form-item>
        <el-form-item label="条码号" prop="barcode">
          <el-input v-model="form.barcode" placeholder="请输入条码号" />
        </el-form-item>
        <el-form-item label="出版社" prop="publisherInfo">
          <el-input v-model="form.publisherInfo" placeholder="请输入出版社" />
        </el-form-item>
        <el-form-item label="ISBN" prop="isbn">
          <el-input v-model="form.isbn" placeholder="请输入ISBN" />
        </el-form-item>
        <el-form-item label="封面" prop="bookCoverUrl">
          <el-input v-model="form.bookCoverUrl" placeholder="请输入封面" />
        </el-form-item>
        <el-form-item label="书籍位置" prop="locationInfo">
          <el-input v-model="form.locationInfo" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="委托人ID" prop="userId">
          <el-input v-model="form.userId" placeholder="请输入委托人ID" />
        </el-form-item>
        <el-form-item label="委托人" prop="userName">
          <el-input v-model="form.userName" placeholder="请输入委托人" />
        </el-form-item>
        <el-form-item label="委托电话" prop="userPhone">
          <el-input v-model="form.userPhone" placeholder="请输入委托电话" />
        </el-form-item>
        <el-form-item label="开始时间" prop="deliveryPeriodStart">
          <el-date-picker clearable v-model="form.deliveryPeriodStart" type="date" value-format="yyyy-MM-dd"
            placeholder="请选择开始时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="截止时间" prop="deliveryPeriodEnd">
          <el-date-picker clearable v-model="form.deliveryPeriodEnd" type="date" value-format="yyyy-MM-dd"
            placeholder="请选择截止时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="目的地" prop="deliveryLocation">
          <el-input v-model="form.deliveryLocation" placeholder="请输入目的地" />
        </el-form-item>
        <el-form-item label="备注" prop="notes">
          <el-input v-model="form.notes" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="接单人ID" prop="acceptorId">
          <el-input v-model="form.acceptorId" placeholder="请输入接单人ID" />
        </el-form-item>
        <el-form-item label="接单学号" prop="acceptorStudentId">
          <el-input v-model="form.acceptorStudentId" placeholder="请输入接单学号" />
        </el-form-item>
        <el-form-item label="接单人" prop="acceptorName">
          <el-input v-model="form.acceptorName" placeholder="请输入接单人" />
        </el-form-item>
        <el-form-item label="接单电话" prop="acceptorPhone">
          <el-input v-model="form.acceptorPhone" placeholder="请输入接单电话" />
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-radio-group v-model="form.status">
            <el-radio v-for="dict in dict.type.book_commission" :key="dict.value" :label="parseInt(dict.value)">{{
              dict.label }}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="评分" prop="rating">
          <el-input v-model="form.rating" placeholder="请输入评分" />
        </el-form-item>
        <el-form-item label="评价" prop="review">
          <el-input v-model="form.review" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="创建时间" prop="createdAt">
          <el-date-picker clearable v-model="form.createdAt" type="date" value-format="yyyy-MM-dd"
            placeholder="请选择创建时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="更新时间" prop="updatedAt">
          <el-date-picker clearable v-model="form.updatedAt" type="date" value-format="yyyy-MM-dd"
            placeholder="请选择更新时间">
          </el-date-picker>
        </el-form-item>
        <!-- 学校选择列表 -->
        <el-form-item label="学校" prop="institutionId">
          <el-select v-model="form.institutionId" filterable placeholder="请选择学校" style="width: 100%">
            <el-option v-for="item in institutionList" :key="item.institutionId" :label="item.institutionName"
              :value="item.institutionId" />
          </el-select>
        </el-form-item>

        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="primary" icon="el-icon-plus" size="mini"
              @click="handleAddBookInstitutionInfo">添加</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="danger" icon="el-icon-delete" size="mini"
              @click="handleDeleteBookInstitutionInfo">删除</el-button>
          </el-col>
        </el-row>
        
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listCommission, getCommission, delCommission, addCommission, updateCommission } from "@/api/book/commission";
import { listInstitutions } from "@/api/book/institutions";

export default {
  name: "Commission",
  dicts: ['book_commission'],
  data() {
    return {
      // 遮罩层
      loading: true,
      loadingInstitution: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 图书委托表格数据
      commissionList: [],
      // 机构信息表格数据
      institutionList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        bookTitle: null,
        locationInfo: null,
        userName: null,
        deliveryPeriodStart: null,
        deliveryPeriodEnd: null,
        deliveryLocation: null,
        acceptorName: null,
        status: null,
        rating: null,
        institutionId: null,
        institutionName: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        bookTitle: [
          { required: true, message: "书名不能为空", trigger: "blur" }
        ],
        userId: [
          { required: true, message: "委托人ID不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
    this.getInstitutionList();
  },
  methods: {
    /** 查询图书委托列表 */
    getList() {
      this.loading = true;
      listCommission(this.queryParams).then(response => {
        this.commissionList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 查询机构(学校)列表 */
    getInstitutionList() {
      this.loadingInstitution = true;
      listInstitutions(this.queryParams).then(response => {
        this.institutionList = response.rows;
        this.totalInstitution = response.total;
        this.loadingInstitution = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        commissionId: null,
        bookTitle: null,
        callNumber: null,
        barcode: null,
        publisherInfo: null,
        isbn: null,
        bookCoverUrl: null,
        locationInfo: null,
        userId: null,
        userName: null,
        userPhone: null,
        deliveryPeriodStart: null,
        deliveryPeriodEnd: null,
        deliveryLocation: null,
        notes: null,
        acceptorId: null,
        acceptorStudentId: null,
        acceptorName: null,
        acceptorPhone: null,
        status: null,
        rating: null,
        review: null,
        createdAt: null,
        updatedAt: null,
        institutionId: null,
        institutionName: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中委托数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.commissionId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加图书委托";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const commissionId = row.commissionId || this.ids
      getCommission(commissionId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改图书委托";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.form.bookInstitutionInfoList = this.bookInstitutionInfoList;
          if (this.form.commissionId != null) {
            updateCommission(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addCommission(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const commissionIds = row.commissionId || this.ids;
      this.$modal.confirm('是否确认删除图书委托编号为"' + commissionIds + '"的数据项？').then(function () {
        return delCommission(commissionIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('book/commission/export', {
        ...this.queryParams
      }, `commission_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
